package liuhao.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import liuhao.account.dao.AccountDao;
import liuhao.account.entity.AccountEntity;
import liuhao.account.service.AccountService;
import liuhao.bank.dao.BankDao;
import liuhao.base.SessionUtil;
import liuhao.dto.PageDTO;

public class AccountServiceImpl implements AccountService {


	@Override
	public Map<String, Object> findById(AccountEntity entity)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		AccountDao dao = session.getMapper(AccountDao.class);
		
		try {
			AccountEntity accountEntity = dao.findById(entity);
		
			if(null !=accountEntity){
				map.put("code", "200");
				map.put("accountEntity", accountEntity);
			}else{
				map.put("code", "333");
				map.put("accountEntity", "null");
			}
		} catch (Exception e) {
			
			//业务失败时的操作
			map.put("code", "333");
			map.put("accountEntity", "null");
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> findByName(AccountEntity entity) throws Exception {
		
		return null;
	}

	
	@Override
	public Map<String, Object> findAccounts(PageDTO page)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		AccountDao dao = session.getMapper(AccountDao.class);
		List<AccountEntity> listAccounts;
		try {
		
		listAccounts = dao.findAccounts(page);
		//2获得查询的条数
		int acount = dao.queryPageAcount();
		
		//获得分页的总数目
		int pageSide = page.getPageSide();
		//如果不能整除，页数加一，否则等于页数除以分页的数
		int yeshu=acount % pageSide==0?acount/pageSide:acount/pageSide+1;
//		System.out.println(yeshu);
		
		
		map.put("messge", "操作成功");
		map.put("fingWithPage", listAccounts);
		map.put("yeshu", yeshu);
		
		//获得当前的页数
		map.put("page",page);
		
	} catch (Exception e) {
		
		//业务失败时的操作
		map.put("messge", "操作失败");
		//获得当前的页数
		map.put("page",page);
		
		e.printStackTrace();
	}
		return map;
	}

	@Override
	public Map<String, Object> updateAccountById(AccountEntity entity)  {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		
		AccountDao dao = session.getMapper(AccountDao.class);
		
		//对bank表进行查询，得到银行的id
		BankDao bankDao = session.getMapper(BankDao.class);
		String bankname=entity.getBankName();
		try {
			
			int banckId = bankDao.findBankByName(bankname);
			//set新修改的银行的id
			entity.setBankId(banckId);
			int addBank = dao.updateAccountById(entity);
		
			if(addBank==1){
				map.put("code", "200");
			}else{
				map.put("code", "333");
			}
		} catch (Exception e) {
			
			//业务失败时的操作
			map.put("code", "333");
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> deleteAccountById(String[] delid) {
		
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		AccountDao dao = session.getMapper(AccountDao.class);
		
		int counter=0;
		for(int i=0;i<delid.length;i++){
			int did = Integer.parseInt(delid[i]);
			
			try {
				int id = dao.deleteAccountById(did);
				if(id>=1){
					//删除成功返回
					map.put("code","200");
					//成功删除的条数
					counter++;
					map.put("mycounter",counter);
				}else{
					//删除失败返回
					map.put("code","333");
				}
			} catch (Exception e) {
				map.put("code",333);
				e.printStackTrace();
			}
		}
		return map;
	}

	@Override
	public Map<String, Object> addAccountById(AccountEntity entity){
		Map<String, Object> map=new HashMap<String, Object>();
		SqlSession session = SessionUtil.getSession();
		AccountDao dao = session.getMapper(AccountDao.class);
		BankDao bankdao = session.getMapper(BankDao.class);
		
		try {
		
			int bankId = bankdao.findBankByName(entity.getBankName());
			entity.setBankId(bankId);
			
			 int addBank = dao.addAccountById(entity);
			if(addBank==1){
				
				map.put("code", "200");
			}else{
				map.put("code", "333");
			}
		} catch (Exception e) {
			//业务失败时的操作
			map.put("code", "333");
			e.printStackTrace();
		}
		return map;
	}

}
